import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/generated/assets.dart';
import 'package:flutter_base/widgets/image/auto_image.dart';

///  左右按钮标题栏
class CommonAppBar extends StatelessWidget implements PreferredSizeWidget {
  const CommonAppBar({
    super.key,
    required this.title,
    this.titleColor,
    this.onLeftClick,
    this.onRightClick,
    this.centerTitle,
    this.showRight = false,
    this.showLeft = true,
    this.actions,
    this.leadingImage,
    this.leadingImageColor,
    this.backgroundColor = Colors.transparent,
    this.focusNode,
    this.titleWidget,
    this.systemOverlayStyle = SystemUiOverlayStyle.dark,
    this.titleSpacing,
    this.leftWidget,
    // 141416
  });

  final Function()? onLeftClick;
  final Function()? onRightClick;
  final String title;
  final Color? titleColor;
  final bool? centerTitle;
  final bool? showRight;
  final bool? showLeft;
  final Widget? leftWidget;
  final List<Widget>? actions;
  final String? leadingImage;
  final Color? leadingImageColor;
  final Color backgroundColor;
  final Widget? titleWidget;
  final FocusNode? focusNode;
  final SystemUiOverlayStyle? systemOverlayStyle;
  final double? titleSpacing;

  @override
  Widget build(BuildContext context) {
    return _buildAppBar();
  }

  Widget _buildAppBar() {
    return AppBar(
      scrolledUnderElevation: 0,
      //滑动渐变
      title: titleWidget ??
          Text(
            title,
            style: TextStyle(
              color: titleColor ?? AppColor.color282828,
              fontSize: 18.sp,
              fontWeight: FontWeight.w600,
            ),
          ),
      centerTitle: centerTitle ?? true,
      leading: showLeft == true
          ? (leftWidget ?? _buildLeftButton())
          : const SizedBox(),
      automaticallyImplyLeading: showLeft == false,
      // leadingWidth: 200.w,
      leadingWidth: showLeft == false ? 0 : (leftWidget != null ? 100.w : 44.w),
      titleSpacing: titleSpacing ?? 16.w,
      actions: actions ?? [_buildRightButton()],
      backgroundColor: backgroundColor,
      toolbarHeight: 44.w,
      systemOverlayStyle: systemOverlayStyle,
    );
  }

  _buildLeftButton() {
    return GestureDetector(
      onTap: () {
        FocusManager.instance.primaryFocus?.unfocus();
        if (onLeftClick != null) {
          onLeftClick?.call();
        } else {
          Get.back();
        }
      },
      behavior: HitTestBehavior.opaque,
      child: Container(
        width: 44.w,
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.only(left: 16.w),
        child: AutoImage(
          leadingImage ?? Assets.commonBarBack,
          width: 24.w,
          color: leadingImageColor,
        ),

      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(44.w);

  _buildRightButton() {
    if (showRight != null && showRight!) {
      return GestureDetector(
        onTap: onRightClick,
        child: Container(
          margin: const EdgeInsets.only(right: 16),
          child: const Icon(
            Icons.search,
            color: Colors.white,
          ),
        ),
      );
    }
    return Container();
  }
}
